<template>
	<view>
		<!-- 我的基本信息 -->
		<view class="myInfo">
			<u-avatar class="headPicture" :src="image" size="80" @click="goToImage"></u-avatar>
			<view style="font-weight: 800;" @click="editName">
			{{name}}
			<text v-if="name==''">点击添加群名</text> 
			</view>
		</view>

		<view @click="send" class="send">
			新建
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				image: "",
				name: ""
			};
		},
		methods: {
			goToImage() {
				let that = this;
				uni.chooseImage({
					sourceType: ['album'], //从相册选择
					count: 1,
					success(res) {
						let filePath = res.tempFilePaths[0]
						uniCloud.uploadFile({
							filePath: filePath,
							cloudPath: Math.floor(Math.random() * 900000000) + 100000000 + '.jpg',
							success(res) {
								that.image = res.fileID;
							}
						});
					}
				});
			},
			editName() {
				let that = this;
				uni.showModal({
					title: "请输入要更改的群名称：",
					editable: true,
					success: function(res) {
						if (res.confirm) {
							if (res.content == "") {
								uni.showToast({
									icon: "error",
									title: "群名称为空"
								})
							} else {
								that.name = res.content;
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			send() {
				if (this.name == "" || this.image == "") {
					uni.showToast({
						icon: 'error',
						title: "请将信息填写完整"
					})
				} else {
					uni.request({
						url: getApp().url + 'team/addTeam',
						data: {
							teamId: Math.floor(Math.random() * 900000000) + 1,
							name: this.name,
							image: this.image,
							adminId: uni.getStorageSync("userId"),
							enterpriseId: uni.getStorageSync("enterprise").enterpriseId
						},
						header: {
							token: uni.getStorageSync("token")
						},
						method: "POST",
						success: (response) => {
							if (response.data.code == -1) {
								return uni.showToast({
									icon: "error",
									title: response.data.msg
								});
							};
							uni.navigateBack();
						},
						fail: (error) => {
							console.log(error);
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.myInfo {
		margin: 10vw 5vw 0vw 5vw;
		padding: 0 5vw 5vw 5vw;
		box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
		border-radius: 10px;
		display: flex;
		flex-direction: column;
		align-items: center;

		.headPicture {
			position: relative;
			bottom: 5vw;
		}
	}

	.send {
		margin: 30px 5vw;
		height: 40px;
		line-height: 40px;
		border-radius: 20px;
		text-align: center;
		color: #FFF;
		background-color: #41A863;
	}
</style>